﻿#include "../lv_Agriculture.h"

lv_obj_t* lv_MqttSend_Screen;
lv_obj_t* Data_Display;
lv_obj_t* Input;
static void lv_MqttSend_Content(lv_obj_t* parent);
static void Sidebar(lv_obj_t* parent);
static void Data(lv_obj_t* parent);
void lv_MqttSendExit_Btn(lv_obj_t* parent);
void lv_MqttSendExit_Btn_Callback(lv_event_t* e);

lv_group_t * mqttsend_group;
void lv_MqttSend_Page(void)
{
    lv_MqttSend_Screen = lv_obj_create(NULL);
    lv_obj_add_style(lv_MqttSend_Screen, &lv_Clean_Style, LV_STATE_DEFAULT);
    lv_obj_clear_flag(lv_MqttSend_Screen, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(lv_MqttSend_Screen, 320, 240);
    lv_Top_StateBar(lv_MqttSend_Screen);
    lv_MqttSend_Content(lv_MqttSend_Screen);
    lv_MqttSendExit_Btn(lv_MqttSend_Screen);
}

static void lv_MqttSend_Content(lv_obj_t* parent)
{
    lv_obj_t* MqttSend_Content = lv_obj_create(parent);
    lv_obj_add_style(MqttSend_Content, &lv_Clean_Style, LV_STATE_DEFAULT);
    lv_obj_set_flex_flow(MqttSend_Content, LV_FLEX_FLOW_ROW_WRAP);
    lv_obj_set_align(MqttSend_Content, LV_ALIGN_BOTTOM_MID);
    lv_obj_set_size(MqttSend_Content, lv_pct(100), lv_pct(85));
    lv_obj_set_flex_flow(MqttSend_Content, LV_FLEX_FLOW_ROW);
    lv_obj_clear_flag(MqttSend_Content, LV_OBJ_FLAG_SCROLLABLE);

    Sidebar(MqttSend_Content);
    Data(MqttSend_Content);
}


static void Sidebar(lv_obj_t* parent)
{
    lv_obj_t* Sidebar = lv_obj_create(parent);
    lv_obj_set_align(Sidebar, LV_ALIGN_BOTTOM_LEFT);
    lv_obj_set_size(Sidebar, lv_pct(25), lv_pct(100));
}
static void Data(lv_obj_t* parent)
{
    mqttsend_group = lv_group_create();
    lv_indev_set_group(indev_keypad, mqttsend_group);
    
    lv_obj_t* Data = lv_obj_create(parent);
    lv_obj_add_style(Data, &lv_Clean_Style, LV_STATE_DEFAULT);
    lv_obj_set_align(Data, LV_ALIGN_BOTTOM_RIGHT);
    lv_obj_set_size(Data, lv_pct(73), lv_pct(100));

    Data_Display = lv_obj_create(Data);
    lv_obj_set_align(Data_Display, LV_ALIGN_TOP_LEFT);
    lv_obj_set_size(Data_Display, lv_pct(100), lv_pct(80));
    lv_obj_set_flex_flow(Data_Display, LV_FLEX_FLOW_COLUMN);
    lv_MqttSend_ADDMessage("qnia sadas");
    lv_MqttSend_ADDMessage("1nia sadas");
 
    Input = lv_textarea_create(Data);
    lv_obj_set_align(Input, LV_ALIGN_BOTTOM_LEFT);
    lv_obj_set_size(Input, lv_pct(80), LV_SIZE_CONTENT);
    lv_textarea_set_one_line(Input, true);

    lv_obj_t* Send_Btn = lv_btn_create(Data);
    lv_obj_t* Exit_Label = lv_label_create(Send_Btn);
    lv_label_set_text(Exit_Label, LV_SYMBOL_RIGHT);
    lv_obj_set_align(Send_Btn, LV_ALIGN_BOTTOM_RIGHT);
    lv_obj_add_event_cb(Send_Btn,SendBtn_Callback,LV_EVENT_CLICKED,NULL);

    lv_obj_t* KeyBorad = lv_keyboard_create(lv_layer_top());
    lv_keyboard_set_textarea(KeyBorad, Input);
    lv_obj_add_flag(KeyBorad, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_event_cb(Input, KeyBorad_Callback, LV_EVENT_ALL, KeyBorad);

    lv_group_add_obj(mqttsend_group,Input);
    lv_group_add_obj(mqttsend_group,Send_Btn);
    lv_group_add_obj(mqttsend_group,KeyBorad);
}


void lv_MqttSend_ADDMessage(const char* Message)
{
    lv_obj_t* Label = lv_label_create(Data_Display);
    lv_obj_set_size(Label, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    lv_label_set_text(Label, Message);
}


void lv_MqttSendExit_Btn(lv_obj_t* parent)
{
    lv_obj_t* Exit_Btn = lv_btn_create(parent);
    lv_obj_t* Exit_Label = lv_label_create(Exit_Btn);
    lv_label_set_text(Exit_Label, LV_SYMBOL_LEFT);
    lv_obj_set_align(Exit_Btn, LV_ALIGN_BOTTOM_LEFT);
    lv_obj_add_event_cb(Exit_Btn, lv_MqttSendExit_Btn_Callback, LV_EVENT_ALL, NULL);
    lv_group_add_obj(mqttsend_group,Exit_Btn);
}

void lv_MqttSendExit_Btn_Callback(lv_event_t* e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t* target = lv_event_get_target(e);

    if (event_code == LV_EVENT_CLICKED) {
        lv_Home_Page();
        lv_scr_load_anim(lv_Home_Screen, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
    }
}
